package Interface;

import java.util.ArrayList;

public class TrackMap {

	int x;
	int y;
	int xdir;
	int ydir;
	int height;
	boolean loop;
	boolean controlloop = false; // true if 5 panels after original loop the panel are still the same
	private int control = 0;

	private final int CONTROLEPANELS = 0; // aantal panelen om te controleren als loop misschien gevonden is.

ArrayList<Integer> hist;
//ArrayList<Integer> travelmap; // map die gevolgd moet worden, begint van index ControlePanels;
ArrayList<Integer> controle;

public TrackMap(){
	this.height = 0;
	 x = 0;
	 y = 0;
	 xdir = 0;
	 ydir = -1;
	 loop = false;
	 controlloop = false;
	 
	 hist = new ArrayList<Integer>();
	 controle = new ArrayList<Integer>();
//	 hist.add(1);
//	 hist.add(1);
//	 hist.add(1);
//	 hist.add(6);
//	 hist.add(1);
//	 hist.add(6);
//	 hist.add(1);
//	 hist.add(1);
//	 hist.add(1);
//	 hist.add(1);
//	 hist.add(1);
//	 hist.add(3);
//	 hist.add(3);
//	 hist.add(1);
//	 hist.add(3);
//	 hist.add(1);
//	 hist.add(1);
//	 hist.add(6);
}

public void update(int b){
	if(loop) {
		if (!controlloop)
		{		
		
			if (control >= CONTROLEPANELS) //5 panels have been checked and are the same as the one in the original loop
				{
					controlloop = true;
					return;
				}
			else
				{
				if ((b == 3 && 3 == hist.get(control))||(b == 6 && 6 == hist.get(control))||((b != 3 && 3 != hist.get(control))&&(b != 6 && 6 != hist.get(control))))
					{
						controle.add(b);
					}
				else
					{
						loop = false;
						for (int panel : controle) {
							hist.add(panel); // add the buffer to the map
						}
						controle = new ArrayList<Integer>();
						control = 0; // next time loop is found try again from 0
						hist.add(b);
				
					}
				}
		}
		else{
			return;
		}
		
		
		
		
		
	}
		
	else
	{
		hist.add(b);
		int t;
		switch(b) {
			case 3:
				t = xdir;
				xdir = ydir;
				ydir = -t;
				break;
			case 6:
				t = xdir;
				xdir = -ydir;
				ydir = t;
				break;
			case 4:
				height++;
				break;
			case 5:
				height--;
				break;
			default:; // covers versmalling,rechtdoor,wip (enkel rechtdoor rijden)
	}
	x+=xdir;
	y+=ydir;
	if(x==0 && y==0 ) loop = true;
	}
	
}
	


public ArrayList<Integer> getMap(){
	/*if (CONTROLEPANELS == 0)
		return hist;
	*/

	return getTravel();
//	return hist;
}

private ArrayList<Integer> getTravel(){
	ArrayList<Integer> list = new ArrayList<Integer>();
	for (int i = 0; i < hist.size()-CONTROLEPANELS; i++) {
		list.add(i, hist.get(i+CONTROLEPANELS));
	}
	for (int i = 0; i < CONTROLEPANELS; i++) {
		list.add(hist.size() - CONTROLEPANELS + i, hist.get(i));
	}

	
	
	list.add(list.get(0));
	list.remove(0);
	

	
	
	return list;
}

	
public boolean completeTrack(){
	return controlloop;
}


	
}
